//
//  PushTaskView.m
//  Safflower
//
//  Created by 袁亮 on 2016/11/30.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "PushTaskView.h"


@interface PushTaskView()<PushtaskFieldDelegate, PushtaskTextDelegate>
{
    CGFloat _bgWidth;
    CGFloat _bgHeight;
}
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIButton *trueButton;

@property (nonatomic, strong) UIButton *addGiftButton;

@end

@implementation PushTaskView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _bgWidth = SCREEN_WIDTH - 120;
        _bgHeight = SCREEN_HEIGHT - 100;
        
        [self makeBGView];
        
        [self makeTaskInfoModule];
        
        [self makeAddPictureModule];
        
        [self makeDescriptionModule];
        
        [self makeSendWordModule];
        
        [self makeTureButton];
    }
    return self;
}

- (void)makeBGView
{
    _bgView = [UIView new];
//    _bgView.backgroundColor = [UIColor yellowColor];
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(_bgWidth);
        make.centerX.mas_equalTo(self);
        make.bottom.mas_equalTo(-44);
    }];
}

- (void)makeTaskInfoModule
{
    UIView *taskInfoView = [UIView new];
    [_bgView addSubview:taskInfoView];
    [taskInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake((_bgWidth - 10) / 2, (_bgHeight - 10) / 2));
    }];
    
    CGFloat infoHeight = ((_bgHeight - 10) / 2 - 15) / 4;
    
    UILabel *taskNameLabel = [UILabel new];
    taskNameLabel.text = @"任务名字";
    taskNameLabel.textColor = [UIColor blackColor];
    [taskInfoView addSubview:taskNameLabel];
    [taskNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(70, infoHeight));
    }];
    
    _taskNameField = [[PushtaskField alloc]initTaskFieldWithStyle:TaskFieldStyleOfNormal];
    _taskNameField.layer.cornerRadius = infoHeight / 2;
    _taskNameField.placeholder = @"输入任务名字";
    [taskInfoView addSubview:_taskNameField];
    [_taskNameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(taskNameLabel.mas_right).mas_offset(10);
        make.right.mas_equalTo(-5);
        make.height.mas_equalTo(infoHeight);
    }];
    
    
    UILabel *taskDateLabel = [UILabel new];
    taskDateLabel.text = @"任务周期";
    taskDateLabel.textColor = [UIColor blackColor];
    [taskInfoView addSubview:taskDateLabel];
    [taskDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(taskNameLabel.mas_bottom).mas_offset(5);
        make.left.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(70, infoHeight));
    }];
    
    _taskStartDateField = [[PushtaskField alloc]initTaskFieldWithStyle:TaskFieldStyleOfSelect];
    _taskStartDateField.layer.cornerRadius = infoHeight / 2;
    _taskStartDateField.placeholder = @"选择任开始时间";
    _taskStartDateField.tag = 101;
    _taskStartDateField.delegate = self;
    [taskInfoView addSubview:_taskStartDateField];
    [_taskStartDateField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_taskNameField.mas_bottom).mas_offset(5);
        make.left.mas_equalTo(taskDateLabel.mas_right).mas_offset(10);
        make.right.mas_equalTo(-5);
        make.height.mas_equalTo(infoHeight);
    }];
    
    _taskEndDateField = [[PushtaskField alloc]initTaskFieldWithStyle:TaskFieldStyleOfSelect];
    _taskEndDateField.layer.cornerRadius = infoHeight / 2;
    _taskEndDateField.placeholder = @"选择任结束时间";
    _taskEndDateField.tag = 102;
    _taskEndDateField.delegate = self;
    [taskInfoView addSubview:_taskEndDateField];
    [_taskEndDateField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_taskStartDateField.mas_bottom).mas_offset(5);
        make.left.mas_equalTo(taskDateLabel.mas_right).mas_offset(10);
        make.right.mas_equalTo(-5);
        make.height.mas_equalTo(infoHeight);
    }];
    
    UILabel *taskFriendLabel = [UILabel new];
    taskFriendLabel.text = @"执  行  人";
    taskFriendLabel.textColor = [UIColor blackColor];
    [taskInfoView addSubview:taskFriendLabel];
    [taskFriendLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(taskDateLabel.mas_bottom).mas_offset(infoHeight+10);
        make.left.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(70, infoHeight));
    }];
    
    _taskFriendField = [[PushtaskField alloc]initTaskFieldWithStyle:TaskFieldStyleOfSelect];
    _taskFriendField.layer.cornerRadius = infoHeight / 2;
    _taskFriendField.placeholder = @"选择任务好友";
    _taskFriendField.tag = 103;
    _taskFriendField.delegate = self;
    [taskInfoView addSubview:_taskFriendField];
    [_taskFriendField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_taskEndDateField.mas_bottom).mas_offset(5);
        make.left.mas_equalTo(taskFriendLabel.mas_right).mas_offset(10);
        make.right.mas_equalTo(-5);
        make.height.mas_equalTo(infoHeight);
    }];
    
//    UILabel *taskUserLabel = [UILabel new];
//    taskUserLabel.text = @"兑  奖  人";
//    taskUserLabel.textColor = [UIColor blackColor];
//    [taskInfoView addSubview:taskUserLabel];
//    [taskUserLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(taskFriendLabel.mas_bottom).mas_offset(5);
//        make.left.mas_equalTo(0);
//        make.size.mas_equalTo(CGSizeMake(70, infoHeight));
//    }];
//    
//    _taskUserField = [[PushtaskField alloc]initTaskFieldWithStyle:TaskFieldStyleOfSelect];
//    _taskUserField.layer.cornerRadius = infoHeight / 2;
//    _taskUserField.placeholder = @"选择任务发起人";
//    _taskUserField.tag = 103;
//    _taskUserField.delegate = self;
//    [taskInfoView addSubview:_taskUserField];
//    [_taskUserField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_taskFriendField.mas_bottom).mas_offset(5);
//        make.left.mas_equalTo(taskUserLabel.mas_right).mas_offset(10);
//        make.right.mas_equalTo(-5);
//        make.height.mas_equalTo(infoHeight);
//    }];
    
}

- (void)makeAddPictureModule
{
    _addGiftButton = [UIButton new];
    _addGiftButton.layer.masksToBounds = YES;
    _addGiftButton.layer.cornerRadius = 5.0;
    _addGiftButton.layer.borderColor = [[UIColor blackColor] CGColor];
    _addGiftButton.layer.borderWidth = 1.0;
    [_addGiftButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [_addGiftButton setImage:[UIImage imageNamed:@"task_add_gift_button"] forState:UIControlStateNormal];
    [_addGiftButton addTarget:self action:@selector(clickAddGift:) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_addGiftButton];
    [_addGiftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake((_bgWidth - 10) / 2, (_bgHeight - 10) / 2));
    }];
}

- (void)makeDescriptionModule
{
    _descriptionText = [PushtaskText new];
    _descriptionText.placeholder = @"任务描述...";
    _descriptionText.tag = 201;
    _descriptionText.delegate = self;
    [_bgView addSubview:_descriptionText];
    [_descriptionText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake((_bgWidth - 10) / 2, (_bgHeight - 10) / 2));
    }];
}

- (void)makeSendWordModule
{
    _sendWordText = [PushtaskText new];
    _sendWordText.placeholder = @"任务寄语...";
    _sendWordText.tag = 202;
    _sendWordText.delegate = self;
    [_bgView addSubview:_sendWordText];
    [_sendWordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake((_bgWidth - 10) / 2, (_bgHeight - 10) / 2));
    }];
}

- (void)makeTureButton
{
    _trueButton = [UIButton new];
    [_trueButton setTitle:@"确定" forState:UIControlStateNormal];
    _trueButton.titleLabel.font = [UIFont systemFontOfSize:25.0f];
    [_trueButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_trueButton addTarget:self action:@selector(clickTrueButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_trueButton];
    [_trueButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.centerX.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(80, 44));
    }];
}

- (void)setGiftImage:(UIImage *)giftImage
{
    _giftImage = giftImage;
    [_addGiftButton setImage:_giftImage forState:UIControlStateNormal];
}


- (void)clickField:(PushtaskField *)field
{
    [self resignKeyboards];
    if (field.tag == 101) {
        if ([self.delegate respondsToSelector:@selector(clickTaskStartDate)]) {
            [self.delegate clickTaskStartDate];
        }
    }else if (field.tag == 102){
        if ([self.delegate respondsToSelector:@selector(clickTaskEndDate)]) {
            [self.delegate clickTaskEndDate];
        }
    }else if (field.tag == 103){
        if ([self.delegate respondsToSelector:@selector(clickTaskFriend)]) {
            [self.delegate clickTaskFriend];
        }
    }
}

- (void)clickAddGift:(UIButton *)button
{
    [self resignKeyboards];
    if ([self.delegate respondsToSelector:@selector(clickUploadGift)]) {
        [self.delegate clickUploadGift];
    }
}

- (void)clickTrueButton:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(clickTrueWithName:description:sendWord:giftImage:)]) {
        [self.delegate clickTrueWithName:_taskNameField.field.text description:_descriptionText.textView.text sendWord:_sendWordText.textView.text giftImage:[UIImage imageToBase64String:_giftImage]];
    }
    
}

- (void)clickTextView:(PushtaskText *)textView text:(NSString *)text
{
    [self resignKeyboards];
    if (textView.tag == 201) {
        if ([self.delegate respondsToSelector:@selector(clickTaskDescription:)]) {
            [self.delegate clickTaskDescription:text];
        }
    }else if (textView.tag == 202){
        if ([self.delegate respondsToSelector:@selector(clickTaskSendWord:)]) {
            [self.delegate clickTaskSendWord:text];
        }
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self resignKeyboards];
}

- (void)resignKeyboards
{
    [_taskNameField.field resignFirstResponder];
}

@end
